package com.maxd.respository.activitties;


import com.maxd.model.RwActivity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

public interface RwActivityRepository extends JpaRepository<RwActivity, Long>, JpaSpecificationExecutor<RwActivity> {
    @Modifying
    @Query("update RwActivity r set r.joinMembers = r.joinMembers + :num where r.id = :id and r.totalMembers >= r.joinMembers + :num")
    Integer joinWithCheck(@Param("id") Long id, @Param("num") Integer num);

    @Transactional
    @Modifying
    @Query("update RwActivity r set r.joinMembers = r.joinMembers - :num where r.id = :id")
    Integer cancel(@Param("id") Long id, @Param("num") Integer num);
}